package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.entity.UmsCouponMemberRelation;
import com.quan.mall.service.UmsCouponMemberRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.UmsCouponVo;
import com.quan.mall.entity.UmsCoupon;
import com.quan.mall.mapper.UmsCouponMapper;
import com.quan.mall.service.UmsCouponService;


@Slf4j
@Service
public class UmsCouponServiceImpl extends MyBaseServiceImpl<UmsCouponMapper, UmsCoupon> implements UmsCouponService {

    @Autowired
	private UmsCouponMapper mapper;

    @Autowired
    private UmsCouponMemberRelationService umsCouponMemberRelationService;

	@Override
    public List<UmsCouponVo> queryList(PageUtils pageUtils, UmsCouponVo vo) {
        QueryWrapper<UmsCoupon> queryWrapper = new QueryWrapper<UmsCoupon>(vo);

        List<UmsCoupon> list = super.list(pageUtils, queryWrapper);
        List<UmsCouponVo> collect = list.stream().map(item -> {
            UmsCouponVo bean = new UmsCouponVo();
            BeanUtils.copyProperties(item, bean);

            // 领取数量
            final int receiveCount = this.umsCouponMemberRelationService.count(new QueryWrapper<UmsCouponMemberRelation>().eq("coupon_id", item.getId()));
            // 使用数量

            final int useCount = this.umsCouponMemberRelationService.count(new QueryWrapper<UmsCouponMemberRelation>().eq("coupon_id", item.getId()).eq("use_status", 1));
            bean.setReceiveCount(receiveCount);
            bean.setUseCount(useCount);

            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, UmsCouponVo vo) {
        QueryWrapper<UmsCoupon> queryWrapper = new QueryWrapper<UmsCoupon>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<UmsCoupon> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<UmsCoupon> records = page.getRecords();

        final List<UmsCoupon> collect = records.stream().map((item) -> {
            UmsCouponVo bean = new UmsCouponVo();
            BeanUtils.copyProperties(item, bean);

            // 领取数量
            final int receiveCount = this.umsCouponMemberRelationService.count(new QueryWrapper<UmsCouponMemberRelation>().eq("coupon_id", item.getId()));
            // 使用数量

            final int useCount = this.umsCouponMemberRelationService.count(new QueryWrapper<UmsCouponMemberRelation>().eq("coupon_id", item.getId()).eq("use_status", 1));
            bean.setReceiveCount(receiveCount);
            bean.setUseCount(useCount);

            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}